package github.flandre.examplemod.core.registry;

import github.flandre.examplemod.core.init.EffectInit;
import github.flandre.examplemod.core.init.EnchantmentInit;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Effect;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.DamageSource;
import net.minecraft.world.Explosion;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)//注册到forge中
public class RegisterPotion {
    @SubscribeEvent
    public static void onLivingDeath(LivingHurtEvent event){
        DamageSource source = event.getSource(); //受到伤害类型
        if(source.getDamageType().equals("fall")) //摔落伤害
        {
            LivingEntity entityLiving = event.getEntityLiving(); //获取到有药水效果的实体
            Effect effect = EffectInit.EXAMPLE_EFFECT.get();
            if(entityLiving.isPotionActive(effect)) //判断是否存在该药水效果
            {
                EffectInstance activePotionEffect = entityLiving.getActivePotionEffect(effect);
                assert activePotionEffect != null;
                int amplifier = activePotionEffect.getAmplifier(); //获取药水效果等级
                event.setAmount(amplifier > 0 ? 0 : event.getAmount()/2); //如果药水等级 > 0 则受到0点伤害
            }
        }


    }
}
